package com.lemo.value;

import org.apache.commons.io.IOUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.context.support.PropertySourcesPlaceholderConfigurer;
import org.springframework.core.env.Environment;
import org.springframework.core.env.PropertySourcesPropertyResolver;
import org.springframework.core.io.Resource;

import java.io.IOException;

@Configuration
@ComponentScan("com.lemo.value")
@PropertySource("classpath:value.properties")
public class ElConfig {

    @Value("I Love You")
    private String normal;

    @Value("#{systemProperties['os.name']}")
    private String osName;

    @Value("#{T(java.lang.Math).random()*100.0}")
    private Double randomNumber;

    @Value("#{demoService.properties}")
    private String fromAnother;

    @Value("http://www.baidu.com")
    private Resource testUrl;

    @Value("${book.name}")
    private String name;

    @Autowired
    private Environment environment;

    @Bean
    public static PropertySourcesPlaceholderConfigurer propertyResolver(){
        return new PropertySourcesPlaceholderConfigurer();
    }

    public void out(){
        System.out.println(normal);
        System.out.println(osName);
        System.out.println(randomNumber);
        System.out.println(fromAnother);
        try {
            System.out.println(IOUtils.toString(testUrl.getInputStream(),"UTF-8"));
        } catch (IOException e) {
            e.printStackTrace();
        }
        System.out.println(name);
        System.out.println(environment.getProperty("book.author"));
    }

}
